
require('../css/purchase/index.less');
require('../css/purchase/common.less');
var $ = require('jquery');

let headerTemplate = require('../view/templateWidget/header.hbs');
let footerTemplate = require('../view/templateWidget/footer.hbs');
let infoRecomTemplate = require('../view/templateWidget/infoRecom.hbs');
let newestTemplate = require('../view/templateWidget/newest.hbs');
let hotAskTemplate = require('../view/templateWidget/hotAsk.hbs');
let navTemplate = require('../view/templateWidget/nav.hbs');
$('header').html(headerTemplate);
$('footer').html(footerTemplate);


require('../helper/header.js');

//tab切换
$('#tabTitle li').each(function(i){
    $('#tabTitle li:eq('+i+')').mouseenter(function(){
        $('#tabTitle li').removeClass('tabSelected');
        $(this).addClass('tabSelected');
        $('.tabCon').hide();
        $('.tabCon:eq('+i+')').show();
    })
})

// 获取最新采购信息
$.ajax({
    type:"get",
    url:"http://purchasing.mhw001.com/getNewPurchasingList",
    dataType:"json",
    success:function(result){
        console.log(result.data);   
        $('#all_left_contInfo').html(newestTemplate({data: result.data}));
        $('#all_left_hotInfo').html(hotAskTemplate({data: result.data}));
        var newRes = result.data.concat(result.data.slice(0,4));
        newRes.splice(0,4);
        // console.log(newRes);
        $('#all_right_contInfo').html(newestTemplate({data: newRes}));
        $('#all_right_hotInfo').html(hotAskTemplate({data: newRes}));
    }
});
// 最新采购信息左边无缝滚动
var area=document.getElementById("left_cont_info");
var lists=document.getElementById("all_left_contInfo");
seamroll(area,lists);
// 最新采购信息右边无缝滚动
var area=document.getElementById("right_cont_info");
var lists=document.getElementById("all_right_contInfo");
seamroll(area,lists);
//急求采购左边无缝滚动
var area=document.getElementById("left_hotBuy");
var lists=document.getElementById("all_left_hotInfo");
seamroll(area,lists);
//急求采购右边无缝滚动
var area=document.getElementById("right_hotBuy");
var lists=document.getElementById("all_right_hotInfo");
seamroll(area,lists);
// 无缝滚动方法
function seamroll(area,lists){
    lists.innerHTML += lists.innerHTML;
    function scrollUp() {
        if (area.scrollTop >= lists.offsetHeight/2) {
            area.scrollTop = 0;
        } else {
            area.scrollTop++
        }
    }
    var time = 100;
    var mytimer = setInterval(scrollUp, time);
    area.onmouseover = function () {
        clearInterval(mytimer);
    }
    area.onmouseout = function () {
        mytimer = setInterval(scrollUp, time);
    }
}

// 采购信息推荐
$.ajax({
    type:"get",
    url:"http://purchasing.mhw001.com/getExcellentPurchasing?cat_id=1215",
    dataType:"json",
    success:function(result){    
        // console.log(result.data.data);
        $('#tabCon1').html(infoRecomTemplate({data: result.data.data}));
        // 模板加载进来之后处理采购类型
        var res=result.data.data;
        for(var i=0;i<res.length;i++){
            var purchaseType=res[i].purchase_type;
            // console.log(purchaseType);
            if(purchaseType=="0"){
                $(".purchaseType").text("现货/标准品");
            }else if(purchaseType=="1"){
                $(".purchaseType").text("加工/定制品");
            }
        }
        showOfferImmedBtn();
    }
});
$.ajax({
    type:"get",
    url:"http://purchasing.mhw001.com/getExcellentPurchasing?cat_id=30",
    dataType:"json",
    success:function(result){    
        // console.log(result.data.data);
        $('#tabCon2').html(infoRecomTemplate({data: result.data.data}));
        // 模板加载进来之后处理采购类型
        var res=result.data.data;
        for(var i=0;i<res.length;i++){
            var purchaseType=res[i].purchase_type;
            // console.log(purchaseType);
            if(purchaseType=="0"){
                $(".purchaseType").text("现货/标准品");
            }else if(purchaseType=="1"){
                $(".purchaseType").text("加工/定制品");
            }
        }
        showOfferImmedBtn();
    }
});
$.ajax({
    type:"get",
    url:"http://purchasing.mhw001.com/getExcellentPurchasing?cat_id=6",
    dataType:"json",
    success:function(result){    
        // console.log(result.data.data);
        $('#tabCon3').html(infoRecomTemplate({data: result.data.data}));
        // 模板加载进来之后处理采购类型
        var res=result.data.data;
        for(var i=0;i<res.length;i++){
            var purchaseType=res[i].purchase_type;
            // console.log(purchaseType);
            if(purchaseType=="0"){
                $(".purchaseType").text("现货/标准品");
            }else if(purchaseType=="1"){
                $(".purchaseType").text("加工/定制品");
            }
        }
        showOfferImmedBtn();
    }
});
$.ajax({
    type:"get",
    url:"http://purchasing.mhw001.com/getExcellentPurchasing?cat_id=1215",
    dataType:"json",
    success:function(result){    
        // console.log(result.data.data);
        $('#tabCon4').html(infoRecomTemplate({data: result.data.data}));
        // 模板加载进来之后处理采购类型
        var res=result.data.data;
        for(var i=0;i<res.length;i++){
            var purchaseType=res[i].purchase_type;
            // console.log(purchaseType);
            if(purchaseType=="0"){
                $(".purchaseType").text("现货/标准品");
            }else if(purchaseType=="1"){
                $(".purchaseType").text("加工/定制品");
            }
        }
        showOfferImmedBtn();
    }
});
$.ajax({
    type:"get",
    url:"http://purchasing.mhw001.com/getExcellentPurchasing?cat_id=6",
    dataType:"json",
    success:function(result){    
        // console.log(result.data.data);
        $('#tabCon5').html(infoRecomTemplate({data: result.data.data}));
        // 模板加载进来之后处理采购类型
        var res=result.data.data;
        for(var i=0;i<res.length;i++){
            var purchaseType=res[i].purchase_type;
            // console.log(purchaseType);
            if(purchaseType=="0"){
                $(".purchaseType").text("现货/标准品");
            }else if(purchaseType=="1"){
                $(".purchaseType").text("加工/定制品");
            }
        }
        showOfferImmedBtn();
    }
});
$.ajax({
    type:"get",
    url:"http://purchasing.mhw001.com/getExcellentPurchasing?cat_id=1215",
    dataType:"json",
    success:function(result){    
        // console.log(result.data.data);
        $('#tabCon6').html(infoRecomTemplate({data: result.data.data}));
        // 模板加载进来之后处理采购类型
        var res=result.data.data;
        for(var i=0;i<res.length;i++){
            var purchaseType=res[i].purchase_type;
            // console.log(purchaseType);
            if(purchaseType=="0"){
                $(".purchaseType").text("现货/标准品");
            }else if(purchaseType=="1"){
                $(".purchaseType").text("加工/定制品");
            }
        }
        showOfferImmedBtn();
    }
});
// 获取nav数据
$.ajax({
    type:"get",
    url:"http://purchasing.mhw001.com/getGoodsCat",
    dataType:"json",
    success:function(result){   
        console.log(result);
        $('#navLists').html(navTemplate({data: result}));
    }
})
function showOfferImmedBtn(){
    // 模板加载之后再处理鼠标进入显示立即报价
    $(".piece_tabCon_info").each(function(i){
        $('.piece_tabCon_info:eq('+i+')').mouseenter(function(){
            $('.piece_tabCon_info').removeClass('changeState');
            $(this).addClass('changeState');
            $('.quoteBtn').hide();
            $('.quoteBtn:eq('+i+')').show();
        });
        $('.piece_tabCon_info:eq('+i+')').mouseleave(function(){
            $('.piece_tabCon_info').removeClass('changeState');
            $('.quoteBtn').hide();
        });
    })
}